//
//  KZProgressView.m
//  KZTools_ObjectC
//
//  Created by 朱凯 on 2019/1/7.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZProgressView.h"

@implementation KZProgressView
{
    CAShapeLayer *_fgLayer;
    CAShapeLayer *_bgLayer;
}  

- (instancetype)init {
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _bgLayer = [CAShapeLayer layer];
    
    _bgLayer.lineCap = kCALineCapRound;
    _bgLayer.strokeColor = [UIColor.blackColor colorWithAlphaComponent:0.3].CGColor;
    _bgLayer.fillColor = UIColor.clearColor.CGColor;
    _bgLayer.strokeEnd = 1;
    [self.layer addSublayer:_bgLayer];
    
    _fgLayer = [CAShapeLayer layer];
    _fgLayer.lineCap = kCALineCapRound;
    _fgLayer.strokeColor = UIColor.whiteColor.CGColor;
    _fgLayer.fillColor = UIColor.clearColor.CGColor;
    [self.layer addSublayer:_fgLayer];

}

- (void)setProgress:(CGFloat)progress {
    
    _progress = progress;
    _fgLayer.strokeEnd = progress;
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _fgLayer.lineWidth = self.height;
    UIBezierPath *path1 = [UIBezierPath bezierPath];
    [path1 moveToPoint:CGPointMake(0, self.height / 2)];
    [path1 addLineToPoint:CGPointMake(self.width, self.height / 2)];
    _fgLayer.path = path1.CGPath;
    
    _bgLayer.lineWidth = self.height;
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(0, self.height / 2)];
    [path2 addLineToPoint:CGPointMake(self.width, self.height / 2)];
    _bgLayer.path = path2.CGPath;
}

@end
